package com.pn.config;

import com.pn.filter.LoginCheckFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.servlet.FilterRegistration;

/**
 * @Author :LY
 * @Description :原生Servlet的配置类
 * @Date :2025/3/19 22:24
 */
@Configuration
public class ServletConfig {
    //注入redis模板
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        //创建FilterRegistration的bean对象
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        //创建自定义的过滤器
        LoginCheckFilter loginCheckFilter = new LoginCheckFilter();
        //手动注入redis模板
        loginCheckFilter.setRedisTemplate(redisTemplate);
        //将自定义的过滤器注册到FilterRegistrationBean中
        filterRegistrationBean.setFilter(loginCheckFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }
}
